package xyz.xtt.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.xtt.admin.entity.TblAsset;
import xyz.xtt.admin.entity.TblAssetTransaction;
import xyz.xtt.admin.entity.TblPayOrder;
import xyz.xtt.admin.entity.TblUser;
import xyz.xtt.admin.mapper.PayOrderMapper;
import xyz.xtt.admin.service.IPayOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.xtt.admin.service.IUserService;
import xyz.xtt.admin.vo.PayOrderVo;

import java.util.Objects;

/**
 * <p>
 * 支付订单主表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-21
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, TblPayOrder> implements IPayOrderService {
    @Autowired
    private IUserService userService;

   public  Page<PayOrderVo> getList(int current, int pageSize, String phone, String appUserId) {
       MPJLambdaWrapper<TblPayOrder> query = JoinWrappers.lambda(TblPayOrder.class)
               .leftJoin(TblAssetTransaction.class, TblAssetTransaction::getOrderId, TblPayOrder::getBusinessRefId)
               .leftJoin(TblAsset.class, TblAsset::getId, TblAssetTransaction::getAssetId)
               .selectAll(TblPayOrder.class)
               .select(TblAssetTransaction::getAssetId)
               .select(TblAsset::getAssetName)
               .orderByDesc(TblPayOrder::getCreateTime);
       if(StringUtils.isNotBlank(phone)){
           try {
               TblUser user = userService.getUserByNameAndPhone("", phone);
               if(Objects.isNull(user)) {
                   return null;
               }
               query.eq(TblPayOrder::getAppUserId, user.getId());
           } catch (Exception e) {
               return null;
           }
       }
       if(StringUtils.isNotBlank(appUserId)){
           query.eq(TblPayOrder::getAppUserId, appUserId);
       }
       return  baseMapper.selectJoinPage(new Page<>(current, pageSize), PayOrderVo.class, query);
   }
}
